package view.manager;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.CourseDao;
import dao.MajorDao;
import dao.ManagerDao;
import model.Course;
import model.Major;
import model.Manager;
import util.StringUtil;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CourseEditInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTable table;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CourseEditInterfrm frame = new CourseEditInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CourseEditInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 590, 556);

		JLabel lblNewLabel = new JLabel("课程名称：");

		textField = new JTextField();
		textField.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Course> arrayList = new ArrayList<Course>();
				ArrayList<Course> List = new ArrayList<Course>();
				arrayList = CourseDao.select();
				for (Course course : arrayList) {
					if (course.getCou_name().contains(textField.getText())) {
						List.add(course);
					}
				}
				filltable(List);
			}
		});
		btnNewButton.setIcon(new ImageIcon(CourseEditInterfrm.class.getResource("/images/select.png")));

		JScrollPane scrollPane = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(141).addComponent(lblNewLabel).addGap(8)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
								.addGap(29).addComponent(btnNewButton))
						.addGroup(groupLayout.createSequentialGroup().addGap(39)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
										.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 493,
												Short.MAX_VALUE))))
				.addContainerGap(41, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(33).addComponent(lblNewLabel))
						.addGroup(groupLayout.createSequentialGroup().addGap(30).addComponent(textField,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup().addGap(26).addComponent(btnNewButton)))
				.addGap(26).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
				.addGap(34).addComponent(panel, GroupLayout.PREFERRED_SIZE, 218, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(25, Short.MAX_VALUE)));

		JLabel lblNewLabel_1 = new JLabel("课程编号：");

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("课程属性：");

		textField_2 = new JTextField();
		textField_2.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("课程名称：");

		textField_3 = new JTextField();
		textField_3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("课程学分：");

		textField_4 = new JTextField();
		textField_4.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("所在系：");

		comboBox = new JComboBox();

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if (StringUtil.isEmpty(textField_3.getText())) {
					JOptionPane.showMessageDialog(null, "课程名称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_3.requestFocus();
					return;
				}
				if (textField_3.getText().length() >= 30) {
					JOptionPane.showMessageDialog(null, "课程名称太长", "提示", JOptionPane.ERROR_MESSAGE);
					textField_3.requestFocus();
					textField_3.selectAll();
					return;
				}
				if (StringUtil.isEmpty(textField_4.getText())) {
					JOptionPane.showMessageDialog(null, "课程学分不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_4.requestFocus();
					return;
				}
				if (!StringUtil.isNum(textField_4.getText()) || Integer.valueOf(textField_4.getText()) >= 10
						|| Integer.valueOf(textField_4.getText()) <= 0) {
					JOptionPane.showMessageDialog(null, "课程学分需为数字（0-10）", "提示", JOptionPane.ERROR_MESSAGE);
					textField_4.requestFocus();
					return;
				}
				Course course = CourseDao.select(textField_1.getText());
				if (comboBox.getSelectedItem() instanceof Major) {
					Major major = (Major) comboBox.getSelectedItem();
					course.setM_id(major.getM_id());
				}
				course.setCou_category(textField_2.getText());
				course.setCou_name(textField_3.getText());
				course.setCou_credit(textField_4.getText());
				if (CourseDao.updata(course) > 0) {
					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					filltable(CourseDao.select());
					return;
				}
				JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(CourseEditInterfrm.class.getResource("/images/updata.png")));
		btnNewButton_1.setSelectedIcon(new ImageIcon(CourseEditInterfrm.class.getResource("/images/updata.png")));

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				int res = JOptionPane.showConfirmDialog(null, "是否确定删除");
				if (res == 0) {
					if (CourseDao.delete(textField_1.getText()) > 0) {
						JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						textField_1.setText("");
						textField_2.setText("");
						textField_3.setText("");
						textField_4.setText("");
						filltable(CourseDao.select());
						return;
					}
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(CourseEditInterfrm.class.getResource("/images/删 除 .png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(36)
							.addComponent(lblNewLabel_1)
							.addGap(6)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_panel.createSequentialGroup()
								.addGap(38)
								.addComponent(lblNewLabel_3)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(textField_3, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
							.addGroup(gl_panel.createSequentialGroup()
								.addGap(37)
								.addComponent(lblNewLabel_2)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))))
					.addGap(56)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_4)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_5)
							.addGap(18)
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)))
					.addGap(29))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(125)
					.addComponent(btnNewButton_1)
					.addGap(93)
					.addComponent(btnNewButton_2)
					.addContainerGap(101, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(12)
							.addComponent(lblNewLabel_1))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(8)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_4)
								.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(22)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_2)
								.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(21)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_5)
								.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(20)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(btnNewButton_1)
							.addGap(21))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(btnNewButton_2)
							.addGap(20))))
		);
		panel.setLayout(gl_panel);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				/**
				 * b表格点击事件
				 */
				comboBox.removeAllItems();
				int row = table.getSelectedRow();
				textField_1.setText(table.getValueAt(row, 0).toString());
				textField_2.setText(table.getValueAt(row, 1).toString());
				textField_3.setText(table.getValueAt(row, 2).toString());
				textField_4.setText(table.getValueAt(row, 3).toString());
				fillCourseType(String.valueOf(table.getValueAt(row, 4)));
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
			},
			new String[] {
				"\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u5C5E\u6027", "\u8BFE\u7A0B\u540D", "\u5B66\u5206", "\u6240\u5728\u7CFB"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(2).setPreferredWidth(124);
		table.getColumnModel().getColumn(3).setPreferredWidth(59);
		table.getColumnModel().getColumn(4).setPreferredWidth(121);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		filltable(CourseDao.select());
	}

	private void filltable(ArrayList<Course> arrayList) {
		Major major;
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		for (Course course : arrayList) {
			Vector v = new Vector();
			v.add(course.getCou_id());
			v.add(course.getCou_category());
			v.add(course.getCou_name());
			v.add(course.getCou_credit());
			major = MajorDao.select(course.getM_id());
			v.add(major.getM_name());
			dtm.addRow(v);
		}
	}

	/**
	 * 初始化下拉框
	 */
	private void fillCourseType(String firstComboBox) {
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major major : arrayList) {
			if (firstComboBox.equals(major.getM_name())) {
				comboBox.addItem(major);
			}
		}
		for (Major major : arrayList) {
			if (!firstComboBox.equals(major.getM_name())) {
				comboBox.addItem(major);
			}
		}
	}

}
